package com.gym.code.generator.impl.service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;

import com.gym.code.config.admin.bean.method.MethodBaseMappingBean.MethodParameterCombineType;
import com.gym.code.generator.Contants;
import com.gym.code.generator.EnumContants.FieldClassType;
import com.gym.code.generator.model.mapping.field.EnumFieldGenerateModel;
import com.gym.code.generator.model.mapping.field.FieldGenerateModel;
import com.gym.code.generator.model.mapping.field.ParameterFieldGenerateModel;
import com.gym.code.generator.model.mapping.field.ParameterListValueFieldGenerateModel;
import com.gym.code.generator.model.mapping.field.ParameterRangeFieldGenerateModel;
import com.gym.code.generator.model.mapping.layer.service.ServiceLayerGenerateModel;
import com.gym.code.generator.model.mapping.method.AbstractMethodGenerateModel;
import com.gym.code.generator.model.mapping.method.MethodDeleteModel;
import com.gym.code.generator.model.mapping.method.MethodInsertModel;
import com.gym.code.generator.model.mapping.method.MethodSelectModel;
import com.gym.code.generator.model.mapping.method.MethodUpdateModel;
import com.gym.code.generator.utils.LocalStringUtils;

/**
 * 
 * @author xinchun.wang 
   @email: 532002108@qq.com
 * @createTime 2015-4-12 上午11:30:51
 */
public class ServiceImplGenerator extends AbstractServiceGenerator<ServiceLayerGenerateModel>{

	 private static ServiceImplGenerator instance = new ServiceImplGenerator();
	 
		public static ServiceImplGenerator getInstance(){
			return instance;
		}

	
	@Override
	protected List<String> generateClassBegin(ServiceLayerGenerateModel bean) {
		List<String> result = new ArrayList<String>();
		result.add("@Service");
		result.add("public class " + getServiceName(bean) + " implements " + getIServiceName(bean) + " {");
		return result;
	}

	@Override
	protected List<String> generateFields(ServiceLayerGenerateModel bean) {
		List<String> result = new ArrayList<String>();
		StringBuilder sb = new StringBuilder();
		sb.append("@Autowired  \n");
		sb.append("@Qualifier \n");
		sb.append("private ").append(bean.getDaoFaceName()).append(" ").append(bean.getDaoFaceNameLower()).append(";");
		result.add(sb.toString());
		return result;
	}

	@Override
	protected List<String> generateMethods(ServiceLayerGenerateModel bean) {
		List<String> result = new ArrayList<String>();
		for(AbstractMethodGenerateModel method : bean.getMethodModelList()){
			if(method instanceof  MethodDeleteModel){
				result.addAll(generateDeleteMethod(bean,(MethodDeleteModel)method));
			} else if(method instanceof  MethodUpdateModel){
				result.addAll(generateUpdateMethod(bean,(MethodUpdateModel)method));
			} else if(method instanceof  MethodSelectModel){
				result.addAll(generateSelectMethod(bean,(MethodSelectModel)method));
			} else if(method instanceof  MethodInsertModel){
				result.addAll(generateInsertMethod(bean,(MethodInsertModel)method));
			}
		}
		return result;
	}
	
	private List<String> generateInsertMethod(ServiceLayerGenerateModel bean,MethodInsertModel methodModel) {
		StringBuilder methodBuilder = new StringBuilder();
		methodBuilder.append(generateMethodDeclare(methodModel)).append(" { \n");
		StringBuilder params = new StringBuilder();
		params.append(generateSelectParams(methodModel));
		methodBuilder.append("return ").append(bean.getDaoFaceNameLower()).append(".").append(methodModel.getMethodName()).append("(").append(generateDaoParamertStr(methodModel)).append(");\n");
		methodBuilder.append("}");
		return Arrays.asList(methodBuilder.toString());
	}
	
	private List<String> generateDeleteMethod(ServiceLayerGenerateModel bean,MethodDeleteModel methodModel) {
		StringBuilder methodBuilder = new StringBuilder();
		methodBuilder.append(generateMethodDeclare(methodModel)).append(" { \n");
		StringBuilder params = new StringBuilder();
		params.append(generateSelectParams(methodModel));
		methodBuilder.append("return ").append(bean.getDaoFaceNameLower()).append(".").append(methodModel.getMethodName()).append("(").append(generateDaoParamertStr(methodModel)).append(");\n");
		methodBuilder.append("}");
		return Arrays.asList(methodBuilder.toString());
	}

	
	private List<String> generateUpdateMethod(ServiceLayerGenerateModel bean,MethodUpdateModel methodModel) {
		StringBuilder methodBuilder = new StringBuilder();
		methodBuilder.append(generateMethodDeclare(methodModel)).append(" { \n");
		StringBuilder params = new StringBuilder();
		params.append(generateSelectParams(methodModel));
		methodBuilder.append("return ").append(bean.getDaoFaceNameLower()).append(".").append(methodModel.getMethodName()).append("(").append(generateDaoParamertStr(methodModel)).append(");\n");
		methodBuilder.append("}");
		return Arrays.asList(methodBuilder.toString());
	}
	
	private List<String> generateSelectMethod(ServiceLayerGenerateModel bean,MethodSelectModel methodModel) {
		List<String> result = new ArrayList<String>();
		StringBuilder functionMethodBuilder = new StringBuilder();
		functionMethodBuilder.append(generateSelectMethodDeclare(methodModel) + " {");
		result.add(functionMethodBuilder.toString());
		result.add(new StringBuilder("return ").append(bean.getDaoFaceNameLower()).append(".").append(methodModel.getMethodName()).append("(").append(generateDaoParamertStr(methodModel)).append(");").toString());
		result.add("}");
		return result;
	}
	
	
	
	@Override
	protected String generatePackage(ServiceLayerGenerateModel bean) {
		return "package " + bean.getPackageName() + Contants.$lineSplit;
	}

	
}
